import uuid

from app.models.rangeModel import RangeLinkModule
from tools import db


def gen_id():
    return uuid.uuid4()


class Switchs(db.Model):
    __tablename__ = "api_switch"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    range_uuid = db.Column(
        db.String(40), db.ForeignKey(RangeLinkModule.range_uuid, ondelete="CASCADE")
    )
    switch_name = db.Column(db.String(20), index=True, default="交换机")
    subnet_id = db.Column(db.String(120), index=True)
    cidr = db.Column(db.String(120), index=True)
    network_id = db.Column(db.String(120), index=True)


class Routes(db.Model):
    __tablename__ = "api_route"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    op_route = db.Column(db.String(120), index=True)
    range_uuid = db.Column(
        db.String(40), db.ForeignKey(RangeLinkModule.range_uuid, ondelete="CASCADE")
    )
    binding_switchs = db.Column(db.String(255))
    state = db.Column(db.Integer, default=1, comment="是否创建， 1 创建 0  没创建")


class Vpnserver(db.Model):
    __tablename__ = "api_vpn"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    range_uuid = db.Column(
        db.String(40), db.ForeignKey(RangeLinkModule.range_uuid, ondelete="CASCADE")
    )
    vpnserver_id = db.Column(db.String(120), index=True)
    client_username = db.Column(db.String(120), index=True)
    client_passwd = db.Column(db.String(120), index=True)


class SubnetPort(db.Model):
    __tablename__ = "api_subnet_port"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    subnet_id = db.Column(
        db.String(120), db.ForeignKey(Switchs.subnet_id, ondelete="CASCADE")
    )
    port_id = db.Column(db.String(120), index=True)


class Ports(db.Model):
    __tablename__ = "api_ports"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    port_id = db.Column(db.String(36), index=True)
    network_id = db.Column(db.String(36), index=True)
    range_uuid = db.Column(
        db.String(40), db.ForeignKey(RangeLinkModule.range_uuid, ondelete="CASCADE")
    )
    device_id = db.Column(db.String(36), index=True)
    device_owner = db.Column(db.String(255), index=True)
    ip = db.Column(db.String(36), index=True)
    subnet_id = db.Column(db.String(36), index=True)


class RouterTable(db.Model):
    __tablename__ = "api_router_table"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
    router_id = db.Column(
        db.String(36),
        db.ForeignKey(Routes.op_route, ondelete="CASCADE"),
        index=True,
        nullable=False,
    )
    destination_cidr = db.Column(db.String(36), index=True)
    next_hop = db.Column(db.String(36), index=True)


class ActiveSwitchs(db.Model):
    __tablename__ = "api_active_switch"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    range_uuid = db.Column(
        db.String(40), db.ForeignKey(RangeLinkModule.range_uuid, ondelete="CASCADE")
    )
    subnet_id = db.Column(db.String(120), index=True)
    cidr = db.Column(db.String(120), index=True)
    network_id = db.Column(db.String(120), index=True)
    user_id = db.Column(db.String(10), index=True)
    bind_firewall_uuid = db.Column(db.String(120), index=True, comment="绑定的防火墙vmUUID")


class ActiveRoutes(db.Model):
    __tablename__ = "api_active_route"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    op_route = db.Column(db.String(120), index=True)
    range_uuid = db.Column(
        db.String(40), db.ForeignKey(RangeLinkModule.range_uuid, ondelete="CASCADE")
    )
    binding_switchs = db.Column(db.String(10), index=True)
    user_id = db.Column(db.String(10), index=True)


class ActiveVpnserver(db.Model):
    __tablename__ = "api_active_vpn"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    range_uuid = db.Column(
        db.String(40), db.ForeignKey(RangeLinkModule.range_uuid, ondelete="CASCADE")
    )
    vpnserver_id = db.Column(db.String(120), index=True)
    client_username = db.Column(db.String(120), index=True)
    client_passwd = db.Column(db.String(120), index=True)
    user_id = db.Column(db.String(10), index=True)


class ActiveSubnetPort(db.Model):
    __tablename__ = "api_active_subnet_port"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    subnet_id = db.Column(
        db.String(120), db.ForeignKey(Switchs.subnet_id, ondelete="CASCADE")
    )
    port_id = db.Column(db.String(120), index=True)
    user_id = db.Column(db.String(10), index=True)


class ActivePorts(db.Model):
    __tablename__ = "api_active_ports"
    port_id = db.Column(db.String(36), primary_key=True, index=True)
    network_id = db.Column(db.String(36), index=True)
    range_uuid = db.Column(
        db.String(40), db.ForeignKey(RangeLinkModule.range_uuid, ondelete="CASCADE")
    )
    device_id = db.Column(db.String(36), index=True)
    device_owner = db.Column(db.String(255), index=True)
    ip = db.Column(db.String(36), index=True)
    subnet_id = db.Column(db.String(36), index=True)
    user_id = db.Column(db.String(10), index=True)


class ActiveRouterTable(db.Model):
    __tablename__ = "api_active_router_table"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
    router_id = db.Column(
        db.String(36),
        # db.ForeignKey(Routes.op_route, ondelete="CASCADE"),
        db.ForeignKey(ActiveRoutes.op_route, ondelete="CASCADE"),
        index=True,
        nullable=False,
    )
    destination_cidr = db.Column(db.String(36), index=True)
    next_hop = db.Column(db.String(36), index=True)
    user_id = db.Column(db.String(10), index=True)
